package com.zeroxml.cms.dao.impl;

import com.wyvoid.core.persistent.hibernate.HibernateDAO;
import com.wyvoid.core.persistent.hibernate.HibernatePage;
import com.zeroxml.cms.bo.Article;
import com.zeroxml.cms.dao.IArticleDAO;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.ArrayList;

/**
 * User: cjp
 * Date: 2007-9-9
 * Time: 19:39:03
 */
public class ArticleDAO extends HibernateDAO<Article> implements IArticleDAO {

    public HibernatePage<Article> getShowOnIndexArticles(int curPage, int pageSize) {
        String hql = "from Article where showOnIndex=? order by createTime desc";
        return doQuery(hql, true, curPage, pageSize, true);
    }

    public HibernatePage<Article> findArticles(long catalogId, String articleName, int curPage, int pageSize) {
        String hql = "from Article where 1=1 ";
        List params = new ArrayList();
        if (catalogId > 0) {
            hql += " and catalog.id=? ";
            params.add(catalogId);
        }
        if (StringUtils.isNotBlank(articleName)) {
            hql += " and name like ? ";
            params.add("%" + articleName + "%");
        }
        hql+=" order by createTime desc";
        return doQuery(hql, true, curPage, pageSize, params.toArray());

    }

    public HibernatePage<Article> getArticles(long tagId,int curPage,int pageSize) {
        String hql="from Article where tags=? order by createTime desc";
        return doQuery(hql,true,curPage,pageSize,tagId);
    }

    public List<Article> getLatestArticles(int articleSize) {
        String hql="from Article order by createTime desc";
        return doQueryLimitList(hql,true,articleSize);
    }

    public List<Article> getRecommedArticles(long catalogId) {
        String hql="from Article where recommed=true ";
        if(catalogId>0){
            hql+=" and catalog.id="+catalogId;
        }
        hql+=" order by createTime desc";
        return doQueryList(hql,true);
    }
}
